package com.example.camerafacedetect.faceframe;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.camerafacedetect.MainActivity;
import com.example.camerafacedetect.R;
import com.example.camerafacedetect.googledetect.GoogleFaceDetectionActivity;

public class FaceFrameActivity extends AppCompatActivity {

    CameraView cameraView;
    FaceView faceView;
    Bitmap fullBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_faceframe);

        if(!hasFrontCamera()) {
            Toast.makeText(this, "没有前置摄像头", Toast.LENGTH_SHORT).show();
            return ;
        }

        initView(this);
        // 开启消费者线程
        startConsumer();
    }

    private void startConsumer(){
        new BitmapConsumer(this, null, this.faceView).start();
    }

    private void initView(Activity context){
        cameraView = (CameraView) findViewById(R.id.camera_view);
        faceView = (FaceView) findViewById(R.id.face_view);
        // 单击退出操作
        faceView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // 如果当前暂停状态，再次点击应该是重新启动
                if(GlobalVariable.pause_flag){
                    GlobalVariable.pause_flag = false; // 设置启动标志位
                    startConsumer(); // 开启消费者
                    cameraView.reset(); // 开启生产者
                }
                // 如果当前状态启动状态，点击要进行人脸检测暂停
                else {
                    Log.e("++++++++", "Click will exit operation");
                    GlobalVariable.pause_flag = true; // 设置暂停标志位
                    GlobalVariable.bitmap_queue.clear(); // 清空消息队列
                    cameraView.pause(); // 通知摄像头停止采集图像
                }
            }
        });
        cameraView.setFaceView(faceView);
        cameraView.setOnFaceDetectedListener(new CameraView.OnFaceDetectedListener() {
            @Override
            public void onFaceDetected(Bitmap bm) {
                //检测到人脸后的回调方法
                fullBitmap = bm;
//                showDialog();
            }
        });
    }

    /**
     * 判断是否有前置摄像
     * @return
     */
    @SuppressLint("NewApi")
    public static boolean hasFrontCamera(){
        Camera.CameraInfo info = new Camera.CameraInfo();
        int count = Camera.getNumberOfCameras();
        for(int i = 0; i < count; i++){
            Camera.getCameraInfo(i, info);
            if(info.facing == Camera.CameraInfo.CAMERA_FACING_BACK){
                return true;
            }
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
